package com.yitongmao.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VertifyUtils {

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 验证手机格式
     */
    public static boolean IsMobileFormat(String mobiles) {
        /*
         * 移动：134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
         * 联通：130、131、132、152、155、156、185、186 电信：133、153、180、189、（1349卫通）
         * 总结起来就是第一位必定为1，第二位必定为3或5或8，其他位置的可以为0-9
         */
        String telRegex = "^[1][34578]\\d{9}$";// "[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        return !StringUtils.isBlank(mobiles) && mobiles.matches(telRegex);
    }

    /**
     * 验证收货地址
     *
     * @param address
     * @return
     */
    public static boolean isAddressFormat(String address) {
        String addressRegex = "^[^\\s]+$";
        return !TextUtils.isEmpty(addressRegex) && address.matches(addressRegex);
    }

    public static String conversionMobile(String mobile) {
        String str = mobile;
        if (IsMobileFormat(mobile)) {
            str = mobile.substring(0, 3) + "****" + mobile.substring(7);
        }
        return str;
    }

    public static boolean isShowHelp(Context context, boolean isUpdatePkgShow) {
        long last_installed_time;
        boolean isShowHelp;
        SharedPreferences helpMsg = context.getSharedPreferences("helpMsg", 0);
        isShowHelp = helpMsg.getBoolean("isShowHelp", true);
        SharedPreferences.Editor editor = helpMsg.edit();
        if (isUpdatePkgShow) {
            try {
                PackageManager pm = context.getPackageManager();
                ApplicationInfo appInfo;
                appInfo = pm.getApplicationInfo(context.getPackageName(), 0);
                String appFile = appInfo.sourceDir;
                last_installed_time = new File(appFile).lastModified(); // Epoch
                // Time
                long last_installed_time_temp = helpMsg.getLong("last_installed_time", 0);
                Loger.i("AAA", "AAA----last_installed_time_temp:" + last_installed_time_temp);
                if (last_installed_time > last_installed_time_temp) {
                    isShowHelp = true;
                    editor.putLong("last_installed_time", last_installed_time);
                } else {
                    isShowHelp = false;
                }
            } catch (PackageManager.NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                isShowHelp = false;
            }
        }

        editor.putBoolean("isShowHelp", false);
        editor.apply();

        return isShowHelp;
    }

    // 邮箱验证
    public static boolean EmailFormat(String str) {
        Pattern pattern = Pattern
                .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        Matcher mc = pattern.matcher(str);
        return mc.matches();
    }

    //    public static boolean isAPI17() {
//
//        String strVer = android.os.Build.VERSION.RELEASE;
//
//        int nIndex = strVer.indexOf('.', strVer.indexOf('.') + 1);
//
//        if (-1 != nIndex) {
//            strVer = strVer.substring(0, nIndex);
//        }
//
//        return Double.parseDouble(strVer) > 4.1;
//    }
    private static long firstClickTime = 0;
    private static int count = 0;

    //连续点击n次b崩溃操作
    public static void seriesClicks(Context context, int clickTime) {

        if (firstClickTime > 0) {
            long secondClickTime = System.currentTimeMillis();//距离上次开机时间
            long dtime = secondClickTime - firstClickTime;
            if (dtime < 1000) {
                count++;
                ToastUtils.showShort(context, "连续点击了" + count + "次");
                if (count == clickTime) {
                    firstClickTime = 0;
                    count = 0;
                    ToastUtils.showShort(context, "我崩溃了");
                    String value = "12tdf";
                    int valueInt = Integer.valueOf(value);
                }
            } else {
                firstClickTime = 0;
                count = 0;
            }
        } else {
            firstClickTime = 0;
            count = 0;
        }
        firstClickTime = System.currentTimeMillis();
    }

    //是否支持硬件编码，18以上
    public static boolean isSupportHWEncode() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
    }

    //是否支持录制屏幕功能，21以上
    public static boolean isSupportScreenCapture() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
    }

    //是否大于等于16
    public static boolean isSupport16() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
    }

    //是否大于等于19
    public static boolean isSupport19() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    }

    public static int getSystemUiFlags() {
        if (VertifyUtils.isSupport16()) {
            Loger.i("getSystemUiFlags","getSystemUiFlags-isSupport16");
            return View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
        } else if (VertifyUtils.isSupport19()) {
            Loger.i("getSystemUiFlags","getSystemUiFlags-isSupport19");
            return View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        } else {
            Loger.i("getSystemUiFlags","getSystemUiFlags-other");
            return View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        }


    }

}
